import user_agent
import inspect
from Utils.ProxyRequest import ProxyRequest

class Base():
    def __init__(self,logger,proxy_radio):
        self.logger = logger
        self.proxy_radio = proxy_radio

    def __del__(self):
        pass
    def getCurrentFuncName(self):
        # name = sys._getframe().f_code.co_name
        name = inspect.stack()[1][3]
        return name
    def getRandomUa(self):
        ua = user_agent.generate_user_agent()
        return ua
    def get(self,name,url,headers=None):

        if not headers:
            headers = {}
        headers["User-Agent"] = self.getRandomUa()

        try:
            res = ProxyRequest(proxy_radio=self.proxy_radio).get(url=url, headers=headers)
            if 200 == res.status_code:
                # print(type(res.text)) # str
                # print(type(res.content)) # bytes
                return res
            else:
                self.logger.error("%s-status_code=%d|%s" % (name,res.status_code, url))
        except Exception as e:
            self.logger.error("%s-%s|%s" % (name,str(e), url))


